package com.heima.admin.listener;

import com.alibaba.fastjson.JSON;
import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.model.common.constants.NewsAutoScanConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Component
@Slf4j
public class WemediaNewsAutoListener {
    /**
     * 自媒体文章自动审核
     * */
    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;
    /**
     * 用kafka消费注解去拿生产的队列，解析出来后去搞阿里云校验
     * */
    @KafkaListener(topics = NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    /**
     * topics里面写生产者放进去的key
     * recevieMessage收到讯息
     * 打印日志
     * log.info("WemediaNewsAutoListener recevieMessage:{}", message);
     * */
    public void recevieMessage(String message) {
        //判断拿到的消息是否为空
        log.info("WemediaNewsAutoListener recevieMessage :{}", message);
        if (StringUtils.isNotBlank(message)) {
            // 解析消息从kafka拿到的json
            Map map = JSON.parseObject(message, Map.class);
            //拿到map里面的值取到自媒体文章表wm_news表的id
            Object wmNewsId = map.get("wmNewsId");
            //阿里云去审核，自动审核文章
            if (wmNewsId != null) {
                wemediaNewsAutoScanService.autoScanByMediaNewsId((Integer)wmNewsId);
            }
        }
    }
}
